<template>
  <div
    id="previeWindow"
    ref="previeWindow"
    :style="{
      width: printSize.width + 'px',
      height: printSize.height + 'px',
    }"
  >
  </div>
</template>

<script>
export default {
  name: "PreviewFile",
  components: {
  },
  data() {
    return {
      componentsList: [],
      printSize: {},
      scale: 1,
    };
  },
  created() {
  },
  mounted() {

    let _this = this;
    _this.setScale();
    window.addEventListener("keyup", function (e) {
      if (e.keyCode === 32) {
        if (document.fullscreenElement) {
          document.exitFullscreen();
        } else {
          document.requestFullscreen();
        }
      }
    });
    addEventListener("resize", this.debounce(this.setScale));
  },
  methods: {
    getScale() {
      const { width, height } = this.printSize;
      const wh = window.innerHeight / height;
      const ww = window.innerWidth / width;
      return ww < wh ? ww : wh;
    },
    setScale() {
      // 获取到缩放比例，设置它
      this.scale = this.getScale();

      if (this.$refs.previeWindow) {
        this.$refs.previeWindow.style.setProperty("--scale", this.scale);
      }
    },
    debounce(fn, delay) {
      const delays = delay || 500;
      let timer;
      return function () {
        const th = this;
        const args = arguments;
        if (timer) {
          clearTimeout(timer);
        }
        timer = setTimeout(function () {
          timer = null;
          fn.apply(th, args);
        }, delays);
      };
    },
  },
  computed: {

  },
  watch: {
  
  },
};
</script>

<style lang="scss">
#previeWindow {
  --scale: 1;
  position: absolute;
  transform: scale(var(--scale)) translate(-50%, -50%);
  transform-origin: 0 0;
  left: 50%;
  top: 50%;
  transition: 0.3s;
  z-index: 999;
  background-color: rgb(181, 181, 181);
  #previewContainer {
    position: relative;

    .dragItem {
      position: absolute;
      border: .0125rem solid #ebf3ff;
    }
  }
}
</style>
